import Koa from "koa";
import koaBody from "koa-body";
import serve from "koa-static";
import cors from 'koa2-cors';
import { ObjectID } from "mongodb";
import path from 'path';
import { ContextProp, DatabaseProps, ObjTypes } from "./appTypes";
import { ctxRes } from "./middleware/ctxRes";
import router from "./router/router";
//引入日志
import log from "./utils/log";

const app = new Koa();


//http服务器启动
const initHttp = async () => {
  //处理跨域
  app.use(cors());
  /// 静态资源
  app.use(serve(path.join(__dirname, "/public")));


  //注入错误处理和正确返回
  app.use(ctxRes);

  app.use(
    koaBody({
      multipart: true,
      formidable: {
        maxFileSize: 2000 * 1024 * 1024, // 设置上传文件大小最大限制，默认2M
      },
    })
  );
  // 路由控制
  app.use(router.routes());

  // 处理全局异常情况
  process.on("uncaughtException", function (err) {
    log.warn(err);
    log.warn(err.message);
  });
  console.log("listen in 4000");
  console.log("http 服务器创建成功");
  app.listen(4000);
};
initHttp();
